import { Category } from 'src/modules/category/entities/category.entity';
import { Gymnasium } from 'src/modules/gymnasium/entities/gymnasium.entity';
import { Event } from 'src/modules/event/entities/event.entity';
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  OneToMany,
  BeforeInsert,
  BeforeUpdate,
  ManyToMany,
} from 'typeorm';
import { TournamentStatusEnum } from 'src/common/constant/enums';

@Entity('tournament')
export class Tournament {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  address: string;

  @Column()
  fromDate: Date;

  @Column()
  endDate: Date;

  @Column()
  type: number;

  @Column()
  registrationFromDate: Date;

  @Column()
  registrationEndDate: Date;

  @Column()
  status: TournamentStatusEnum;

  @Column()
  adImage: string;

  @Column()
  detailImage: string;

  @Column()
  createBy: number;

  @Column()
  createTime: Date;

  @Column()
  updateBy: number;

  @Column()
  updateTime: Date;

  @ManyToMany(() => Gymnasium)
  gymnasiums: Gymnasium[];

  @OneToMany(() => Category, (category) => category.tournament, {
    cascade: true,
  })
  categories: Category[];

  @OneToMany(() => Event, (event) => event.tournament, {
    cascade: true,
  })
  events: Event[];

  @BeforeInsert()
  BeforeInsert() {
    this.createTime = new Date();
    this.updateTime = this.createTime;
  }

  @BeforeUpdate()
  BeforeUpdate() {
    this.updateTime = new Date();
  }
}
